package com.nerve.secondhandcar.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ko.commons.config.PropertyConfigurer;
import org.ko.core.web.BaseController;
import org.ko.core.websupport.Reply;
import org.ko.core.websupport.log.Log;
import org.ko.core.websupport.log.impl.LogUitls;
import org.ko.core.websupport.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nerve.secondhandcar.common.C;
import com.nerve.secondhandcar.common.bean.ImageResult;
import com.nerve.secondhandcar.controller.model.EvaluatorsModel;
import com.nerve.secondhandcar.domain.Evaluators;
import com.nerve.secondhandcar.domain.Picture;
import com.nerve.secondhandcar.service.EvaluatorsService;
import com.nerve.secondhandcar.service.PictureService;

/**
 * 平台中的测试师对象Controller
 * @author 集成显卡
 * @version 2014-10-14 15:29:15
 */
@Controller
@RequestMapping("/car/evaluators")
public class EvaluatorsController extends BaseController{

	@Autowired
	private EvaluatorsService service;
	@Autowired
	private PictureService picService;
	
	@Override
	protected String getTemplatePath() {
		return "car/evaluators/";
	}

	@RequestMapping(value="add", method = RequestMethod.GET)
	public String add(){
		return view(ADD);
	}
	
	/**
	 * 查看实体
	 * @param req
	 * @param model
	 * @param viewMap
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="view")
	public String view(HttpServletRequest req, EvaluatorsModel model, ModelMap viewMap)
		throws Exception {
		service.view(model);
		
		viewMap.addAttribute(ENTITY, model.getBean());
		return view(VIEW);
	}
	
	@RequestMapping(value="edit",method = RequestMethod.GET)
	public String edit(
			HttpServletRequest request,
			HttpServletResponse response,
			ModelMap model,
			@RequestParam(value = "id", required = true)Long id){
		
		model.addAttribute("entity",service.get(id));
		return view("edit");
	}
	
	/**
	 * 提交修改内容,完成后重定向到列表
	 * @param request
	 * @param response
	 * @param model
	 * @param bean
	 * @return
	 */
	@Log(message="编辑用户,id={0}",params={"id"}, category="Evaluators")
	@RequestMapping(value="edit",method = RequestMethod.POST)
	public String editDo(HttpServletRequest request,HttpServletResponse response,ModelMap model, Evaluators bean){
		
		try{
			service.editDo(bean);
			return "redirect:list.do";
		}catch(Exception e){
			LogUitls.error(e);
			return redirect("edit.do?id="+bean.getId());
		}
	}
	/**
	 * 判断指定的测试师是否存在
	 * @param req
	 * @param model
	 * @return
	 */
	@RequestMapping(value="exist")
	@ResponseBody
	public Reply exist(HttpServletRequest req, EvaluatorsModel model){
		Reply re = new Reply();
		try {
			service.isExist(model);
			re.setMessage(model.getId());
		} catch (Exception e) {
			re.error(e);
		}
		return re;
	}
	
	@Log(message="创建了测试师", category="EVALUATORS")
	@RequestMapping(value="create")
	public String create(HttpServletRequest request, HttpServletResponse response, EvaluatorsModel model){
		try {
			service.create(model);
			
			return redirect("list.do");
		} catch (Exception e) {
			System.err.println("出错啦！");
			e.printStackTrace();
			
			return redirect("list.do");
		}
	}
	
	@Log(message="Ajax创建了测试师", category="EVALUATORS")
	@RequestMapping(value="createAjax")
	@ResponseBody
	public Reply createAjax(HttpServletRequest req, EvaluatorsModel model)
			throws Exception {
		Reply re = new Reply();
		try {
			service.create(model);
			re.setMessage("创建成功");
		} catch (Exception e) {
			re.error(e);
			e.printStackTrace();
		}
		return re;
	}

	@RequiresPermissions("evaluators:delete")
	@Log(message="删除了测试师[id={0}]", params={"id"}, category="Evaluators")
	@RequestMapping(value="delete")
	@ResponseBody
	public Reply delete(HttpServletRequest req, EvaluatorsModel model)
			throws Exception {
		Reply re = new Reply();
		try {
			service.delete(model);
			re.setMessage("删除成功");
		} catch (Exception e) {
			re.error(e);
			e.printStackTrace();
		}
		return re;
	}
	
	
	@RequestMapping(value="list")
	public String list(HttpServletRequest req, EvaluatorsModel model, ModelMap map) throws Exception{
		service.list(model);
		map.addAttribute(PAGINATION, model);
		map.addAttribute("keyword", model.getKeyword());
		return view(LIST);
	}
	
	@RequiresPermissions("evaluators:add")
	@Log(message="上传图片", category="Evaluators")
	@RequestMapping(value="upload",method=RequestMethod.POST)
	public String upload(
			HttpServletRequest req, 
			@RequestParam(value="upload_file",required=false)MultipartFile file, 
			EvaluatorsModel model,
			RedirectAttributes redirectA
			) throws Exception{
		
		try {
			//判断是否上传了图片
			if(file != null && !file.isEmpty()){
				/*
				 * 设置参数
				 */
				ImageResult imgR = new ImageResult();
				imgR.setCreateThumb(false);
				imgR.setDeleteOrigin(true);
				//使用测评师的图片参数
				imgR.setHeight(PropertyConfigurer.getInt(C.UPLOAD_HEIGHT_EVALUATORS, 180));
				imgR.setWidth(PropertyConfigurer.getInt(C.UPLOAD_WIDTH_EVALUATORS, 120));
				
				/*
				 * 设置Picture的属性
				 */
				Picture pic = new Picture();
				pic.setAppId(0);	//appid 为0时，不会保存picture 这个实体
				pic.setCategory(Evaluators.class.getSimpleName());
				picService.dealUploadImage(req, file, imgR, pic);
				//为测评师的头像赋值
				Evaluators evaluators = model.getBean();
				evaluators.setHeader(imgR.getPath());
			}
			
			//保存测评师
			service.create(model);
			
			LogUitls.putLog(file.getOriginalFilename());
			//跳转到测评师列表页面
			return redirect("list.do");
		} catch (Exception e) {
			LogUitls.error(e);
			
			redirectA.addFlashAttribute("error", e.getMessage());
			e.printStackTrace();
		}
		return redirect("add.do");
	}
	
	
	/**
	 * 选择测评师
	 * @param request
	 * @param model
	 * @param page
	 * @param function	回调函数，选择完成后会被调用
	 * @param keyword
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="select")
	public String select(
			HttpServletRequest request, 
			ModelMap model, Page page, 
			//绑定一个参数到function
			//相当于：String function = request.getParam("callback");
			@RequestParam(value="callback", required=false, defaultValue="onSelect")String function, 
			String keyword, 
			String ids
		){
		if(isGET(request)){
			model.addAttribute("func", function);
			model.addAttribute("ids", ids);
			
			return view("select");
		}else{
			model.addAttribute(PAGINATION, service.findForSelect(page, keyword));
			
			return view("select_data");
		}
	}

}